﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.WebControls;
using CavinatorSite.CavinatorService;

namespace CavinatorSite
{
    public partial class NovoItem : System.Web.UI.Page
    {
        public class PerguntasRespondidas
        {
            public string Pergunta { get; set; }

            public string Resposta { get; set; }

            public PerguntasRespondidas(String per, String resp)
            {
                this.Resposta = resp;
                this.Pergunta = per;
            }
        }

        private CavinatorServiceClient _cavinatorService;

        public List<Protocol> ListaPerguntasRespondidas { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            _cavinatorService = (CavinatorServiceClient)Session["Service"];

            if (!Page.IsPostBack)
            {
                //_cavinatorService = (CavinatorServiceClient)Session["Service"];
                ListaPerguntasRespondidas = (List<Protocol>)Session["listaPerguntasRespondidas"];

                #region Perguntas a Responder

                Repeater1.DataSource = _cavinatorService.PerguntasAResponder();
                Repeater1.DataBind();

                Repeater2.DataSource = _cavinatorService.PerguntasRespondidas();
                Repeater2.DataBind();
            }

                #endregion
        }

        public void InsertNewItem()
        {
            //linqConvert
            var listAnswer = (from RepeaterItem p in Repeater1.Items
                              let answered = ((TextBox)p.FindControl("txtAnswer")).Text
                              let hdIdPergunta = ((HiddenField)p.FindControl("hdIdPergunta"))
                              let hdfValue = ((HiddenField)p.FindControl("hdfValue"))
                              select new Answer
                                     {
                                         Question = new Question()
                                                    {
                                                        Id = Convert.ToInt32(hdIdPergunta.Value),
                                                        Value = hdfValue.Value
                                                    },
                                         IdQuestion = Convert.ToInt32(hdIdPergunta.Value),
                                         AnswerValue = answered,
                                     }).ToList();

            var listAnswer2 = (from RepeaterItem p in Repeater2.Items
                               let answered = ((HiddenField)p.FindControl("hdAnswerValue"))
                               let hdQuestionId = ((HiddenField)p.FindControl("hdQuestionId"))
                               let hdQuestionValue = ((HiddenField)p.FindControl("hdQuestionValue"))
                               select new Answer
                               {
                                   Question = new Question()
                                   {
                                       Id = Convert.ToInt32(hdQuestionId.Value),
                                       Value = hdQuestionValue.Value
                                   },
                                   IdQuestion = Convert.ToInt32(hdQuestionId.Value),
                                   AnswerValue = answered.Value,
                               }).ToList();

            listAnswer.AddRange(listAnswer2);

            var insertNewItem = new Item
                                {
                                    Value = txtNomeAnimal.Text,
                                    Answers = listAnswer
                                };

           _cavinatorService.InserirNovoItem(insertNewItem);

           Response.Redirect("~/Home.aspx");

        }

        protected void BtnAddNovoItemClick(object sender, EventArgs e)
        {
            InsertNewItem();
        }

    }
}